package com.daiqee.controller;

import java.io.IOException;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.daiqee.controller.support.BaseController;

/**
 * 文件上传
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月7日 下午5:53:45
 * @package com.daiqee.handler
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Controller  
@RequestMapping("file-upload")  
public class FileUploadController extends BaseController{  
  
    /**
     * 文件上传操作
     * @param request
     * 					请求
     * @param response
     * 					响应
     * @throws IOException
     */
    @RequestMapping(value="/file-upload",method=RequestMethod.POST)
    public void oneFileUpload(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	try{
	        CommonsMultipartResolver cmr = new CommonsMultipartResolver(request.getServletContext());  
	        if (cmr.isMultipart(request)) {  
	            MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) (request);  
	            Iterator<String> files = mRequest.getFileNames();  
	            while (files.hasNext()) {
	            	//mFile:上传上来的文件对象，需要使用getOriginalFilename()来获取文件名称
	                MultipartFile mFile = mRequest.getFile(files.next());  
	                if (mFile != null) {
	                	
	                }  
	            }  
	        }
    	}catch(Exception e){
    		//当文件上传过程中发生异常时执行
			response.getWriter().write("{\"message\":\"上传文件失败\"}");
    	}
    		//文件上传成功时执行,放回json数据格式
			response.getWriter().write("{\"message\":\"上传文件成功\"}");
    }  
} 